#include "cv.h"
#include "highgui.h"
#include "OpenCV_main.h"

using namespace std;

/*
exit(int exitStatus) : exitStatus = 1 is a failure, exitStatus = 0 is a success
*/

/*GLOBAL VARS*/
char arg_one[20];
char arg_two[20];
char program[40];

/*MAIN*/
int main( int argc, char** argv ) {
	
	int choice;				//user choice for the command line menu
	int bad_input;				//bad_input tells us if the input stream (cin) has a bad input
	

	cout << "Welcome to the CSUN UAV 2010 OpenCV Test Program.\n";
	cout << "Please choose from the following to test out certain functions.\n";
	
	while (true){
		
		cout << "----------------------------------------------------------------\n";
		cout << "1. Display lena.jpg.\n";
		cout << "2. Display lena.jpg with 'grain'.\n";
		cout << "3. Display lena.jpg with applied edge detection algorithm.\n";
		cout << "4. Stream video.\n";
		cout << "5. Stream video with applied edge detection algorithm.\n WARNING: Resource intensive.\n";
		cout << "6. Take picture from camera.\n";
		cout << "7. Take picture and compare to snap.jpg (experimental).\n";
		cout << "8. Exit.\n";
		cout << "----------------------------------------------------------------\n";
				
		choice = 0;
		program[0] = 0;
		arg_one[0] = 0;
		arg_two[0] = 0;
		cin >> choice;

			switch (choice){

			case 1 :	strcpy(program, "./display_pic ");
					get_arg(1); 
					//cout << program << "\n";
					system( strncat(program, arg_one, strlen(arg_one)) );
					break;
			case 2 :	strcpy(program, "./grain_pic ");
					get_arg(1); 
					//cout << program << "\n";
					system( strncat(program, arg_one, strlen(arg_one)) );
					break;
			case 3 :	strcpy(program, "./edge_pic ");
					get_arg(1); 
					//cout << program << "\n";
					system( strncat(program, arg_one, strlen(arg_one)) );
					break;
			case 4 :	strcpy(program, "./stream_vid ");
					get_arg(1); 
					//cout << program << "\n";
					system( strncat(program, arg_one, strlen(arg_one)) );
					break;
			case 5 :	strcpy(program, "./edge_vid ");
					get_arg(1); 
					//cout << program << "\n";
					system( strncat(program, arg_one, strlen(arg_one)) );
					system("./edge_vid");
					break;
			case 6 :	strcpy(program, "./snapshot ");
					get_arg(1); 
					//cout << program << "\n";
					system( strncat(program, arg_one, strlen(arg_one)) );
					break;
			case 7 :	strcpy(program, "./snap_image ");
					get_arg(1); 
					//cout << program << "\n";
					system( strncat(program, arg_one, strlen(arg_one)) );
					break;
			case 8 : 	cout << "Thank you for your time. \n";
				 	exit(0);
			default : 	cin.clear();//clear the input stream
					cin.ignore(INT_MAX, '\n');//ignore the error
					cout << "Please enter a choice from the menu.\n";
					break;

			}//end switch
			
	}//end while
	
}//end main

/* DEFINED FUNCTIONS */

void get_arg(int num_args){

	int bad_input;
	char y_n;

/* ASK USER IF THEY WANT TO PASS AN ARGUMENT */		

		if (num_args < 1){		//error-checking
		cout << "get_arg() does not accept a negative amount of arguments";
		return;
		}

		do{
			bad_input = 0;
			cout << "Would you like to pass an arguement? (y/n):\nNote: Selecting No (n) will choose default arguement(s)\n";
			cin >> y_n;
			//cout << y_n;
			if( !cin || (y_n != 'y' && y_n != 'n') ){
				bad_input=1;
				cin.clear();
				cin.ignore(INT_MAX, '\n');
				cout << "Please input the Yes (y) or No (n).\n";
				cout << "----------------------------------------------------------------\n";
			}
		}while(bad_input);
		
		
		if (y_n == 'y'){
		/* GET USER ARGUMENT(S) */

			do{
				bad_input = 0;
				cout << "Enter the file name (1) you wish to use as an arguement:\n";
				cin >> arg_one;
				if(!cin){//checking if there are errors in the input stream
					bad_input=1;
					cin.clear();//clear the input stream
					cin.ignore(INT_MAX, '\n');//ignore the error
					cout << "Please enter a valid argument.\n";
				}
			}while(bad_input);
			
			if (num_args > 1){//if we want to get 2 arguments, get the next argument
			
				do{
					bad_input = 0;
					cout << "Enter the file name (2) you wish to use as an arguement:\n";
					cin >> arg_two;
					if(!cin){
						bad_input=1;
						cin.clear();
						cin.ignore(INT_MAX, '\n');
						cout << "Please enter a valid argument.\n";
					}
				}while(bad_input);
			
			}
			
		
		return;
		}//end if
return;
		
}//end function

/*
TO DO:

possibly get the get_arg() function to take an argument of what name to store the picture as (for 'snapshot')

*/
